package com.source.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.sys.entity.Popedom;
import com.source.sys.entity.Role;
import com.source.sys.mapper.RoleMapper;
import com.source.sys.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.sys.vo.RoleVo;
import com.source.utils.MyPage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Override
    public boolean addRole(Role role) {
        return roleMapper.insert(role)>0?true:false;
    }

    @Override
    public List<Role> listRole() {
        return roleMapper.selectList(new QueryWrapper<Role>().eq("useable", 1));
    }

    @Override
    public Page<Role> listRolePage(int currentPageNo, int size) {
        List<Role> roleList = listRole();
        return MyPage.list2Page(currentPageNo, size,roleList);
    }

    @Override
    public boolean deleteRoleById(String roleId) {
        return roleMapper.logicDeleteRoleById(roleId)>0?true:false;
    }

    @Override
    public Role showRoleById(String roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public boolean updateRole(Role role) {
        return roleMapper.updateById(role)>0?true:false;
    }

    @Override
    public boolean updateRoleNameByRid(String roleId,String roleName) {
        int updateRoleName = roleMapper.updateRoleNameByRid(roleId, roleName);
        if(updateRoleName > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Page<RoleVo> listRolePageByWrapper(String queryContent, Integer currentPageNo, Integer size) {
//        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
//        roleQueryWrapper.like("rid", queryContent).or().like("rname", queryContent);
//        Page<Role> rolePage = roleMapper.selectPage(new Page<>(currentPageNo, size), roleQueryWrapper);
//        List<Role> roleList = roleMapper.selectList(roleQueryWrapper);
//        return MyPage.list2Page(currentPageNo,size,roleList);
        return roleMapper.listRoleVoPageByLike(queryContent,new Page<>(currentPageNo,size));
    }
}
